package com.xu.redis;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

public class Chapter01 {
    private static final int ONE_WEEK_IN_SECONDS = 7 * 86400;
    private static final int VOTE_SCORE = 432;
    private static final int ARTICLES_PER_PAGE = 25;

    public static  void main(String[] args) {
        new Chapter01().run();
    }

    private  void run (){
        Jedis conn = new Jedis("192.168.179.21");
        conn.select(15);
        
        String articleId = postArticle(conn,"xudenghui","X title","http:www.google.com");
        System.out.println("We posted a new article with id: " + articleId);
        System.out.println("Its HASH looks like:");
        Map<String,String> articleData = conn.hgetAll("article:" + articleId);
        for (Map.Entry<String,String> entry : articleData.entrySet()){
            System.out.println("  " + entry.getKey() + ": " + entry.getValue());
        }
    }

    /**
     * 发布文章功能实现
     * @param conn
     * @param user
     * @param title
     * @param link
     * @return
     */
    private String postArticle(Jedis conn, String user, String title, String link) {
        String articleId = String.valueOf(conn.incr("article:"));

        String voted = "voted:"+articleId;
        conn.sadd(voted,user);
        conn.expire(voted,ONE_WEEK_IN_SECONDS);

        long now = System.currentTimeMillis()/1000;
        String article ="article:"+articleId;

        Map<String, String> articleData = new HashMap<>();
        articleData.put("title",title);
        articleData.put("link",link);
        articleData.put("user",user);
        articleData.put("now",String.valueOf(now));
        articleData.put("votes","1");
        conn.hmset(article,articleData);

        conn.zadd("score:",now+VOTE_SCORE,article);
        conn.zadd("time",now,article);

        return article;
    }
}

